var $=function(id){return document.getElementById(id);} /*id方式引用*/
var $N=function(name){return document.getElementsByName(name);} /*name方式引用*/
var $TN=function(tagName){return document.getElementsByTagName(tagName);}  /*tagName方式引用*/
String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");} /*去左右空格*/
String.prototype.ltrim = function(){return this.replace(/^\s*/g, "");}  /*去左空格*/
String.prototype.rtrim = function(){return this.replace(/\s*$/g, "");}  /*去右空格*/


function addListener(el,eventType,fn){
	if(el.addEventListener){el.addEventListener(eventType,fn,false);}
	else if(el.attachEvent){el.attachEvent("on" + eventType,fn);}
	else{el["on"+eventType] = fn;}
}

/**强制重新加载当前URL*/
function reload(){var href = location.href;location.href = randomURL(href);}
/**强制重新加载指定URL*/
function reloadURL(href){location.href = randomURL(href);}
/**添加随机数到URL上*/
function randomURL(href){
	var radVal=parseInt(Math.random()*1000000000);
	href=href.replace(/#$/g,"");
	if(href.indexOf("?") > 0){var reg=/(\?|&)[0-9\.]+$/;if(reg.test(href)){href=href.replace(reg,"$1"+radVal);}else{href+="&"+radVal;}}
	else {href += "?" + radVal;}		
	return href;
}

function formatDate(dateStr){  
    if(dateStr==null||dateStr==""||dateStr=='null') return "";   
	var date=new Date(dateStr);
    var y=date.getFullYear(); /*date.getYear()*/  
    if(y<10) y="0"+y;   
    var m=date.getMonth()+1;   
    if(m<10) m="0"+m;   
    var d=date.getDate();   
    if(d<10) d="0"+d;   
    var redate=y+"-"+m+"-"+d;   
    return redate;   
}

function stopUp(evt){			
	var e=(evt)?evt:window.event; 
	if (window.event){ 
 		e.cancelBubble=true; 
   	} else { 
 		e.stopPropagation(); 
	}
}

var IMG=(function(){
	return {
		setMiddle:function(obj,W,H){		
			var imgObj=obj,_W,_H,image= new Image();
			if(null==W||null==H){
				W=obj.style.width!=""?obj.style.width:(obj.width+"px");
				H=obj.style.height!=""?obj.style.height:(obj.height+"px");
			}else{W+="px";H+="px"}			
			W=parseInt(W.replace('px',''));
			H=parseInt(H.replace('px',''));	
			image.src = imgObj.src;
			
			if(image.width>0 && image.height>0){
				if(image.width>W||image.height>H){	
					if(image.width/image.height >= W/H){_W = W;_H = (image.height*W)/image.width;}
					else{_H = H;_W= (image.width*H)/image.height;}
				}else{_W=image.width;_H=image.height}			
			}else{_W=W;_H=H;}
			
			var mTop= (H == _H)?0:(H - _H - 2) / 2;
			var mLeft= (W==_W)?0:(W - _W ) / 2;
			imgObj.style.width=_W+'px';
			imgObj.style.height=_H+'px';
			imgObj.style.marginTop = mTop + "px";
			imgObj.style.marginLeft = mLeft + "px";	
		},
		setGbMiddle:function(imgName){
			var imgs=document.getElementsByName(imgName);
			for(var i=0;i<imgs.length;i++){this.setMiddle(imgs[i],null,null);}
		}
	}
})();

/*只允许输入数字和小数
onKeyUp="clearNoNum(event,this)" onBlur="checkNum(this)"
*/
function clearNoNum(event,obj){ 
    //响应鼠标事件，允许左右方向键移动 
    event = window.event||event; 
    if(event.keyCode == 37 | event.keyCode == 39){ 
        return; 
    } 
    //先把非数字的都替换掉，除了数字和. 
    obj.value = obj.value.replace(/[^\d.]/g,""); 
    //必须保证第一个为数字而不是. 
    obj.value = obj.value.replace(/^\./g,""); 
    //保证只有出现一个.而没有多个. 
    obj.value = obj.value.replace(/\.{2,}/g,"."); 
    //保证.只出现一次，而不能出现两次以上 
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$","."); 
} 
function checkNum(obj){ 
    //为了去除最后一个. 
    obj.value = obj.value.replace(/\.$/g,""); 
}